package edu.buaa.act.travel.m.page;

import java.util.List;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import edu.buaa.act.travel.m.Constants;
import edu.buaa.act.travel.m.R;
import edu.buaa.act.travel.m.model.RunModel;
import edu.buaa.act.travel.m.model.TripDetailModel;
import edu.buaa.act.travel.m.tpdl.ResortActivity;
import edu.buaa.act.travel.m.tpdl.TpdlActivity;
import edu.buaa.act.travel.m.tpdl.TransitActivity;

public class RunActivity extends BaseActivity {
	
	private Button surroundButton;
	private List<TpdlActivity> mTripDetailList;
	private RunReceiver mRunReceiver;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.run_activity);
        
        setupLeftPanelButton();
        setupRightPanelButton(R.string.title_stop, new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				stopService(new Intent("edu.buaa.act.travel.m.service.Run"));
			}
		});
        
        final String result = getIntent().getStringExtra(Constants.EXTRA_TRIP_DETAILS);
        mTripDetailList = new TripDetailModel(result).getTripDetails();
        surroundButton=(Button)findViewById(R.id.button_surround);
        
        final Intent intent = new Intent(this, SurroundsActivity.class);
        
        surroundButton.setOnClickListener(new Button.OnClickListener(){

			public void onClick(View arg0) {
				Log.v(UI_MODE_SERVICE, "this is btn pressed");
		    	intent.putExtra(Constants.EXTRA_RESULT, result);
		    	startActivity(intent);
			}	
        });
        
        startService(new Intent("edu.buaa.act.travel.m.service.Run"));
        regReceiver();
	}
	
	private void regReceiver() {
		mRunReceiver = new RunReceiver();
		IntentFilter filter = new IntentFilter(Constants.BROADCAST_SERVICE);
		registerReceiver(mRunReceiver, filter);
	}

	private void setup(final String result) {
		final RunModel model = new RunModel(result);
		final int itemSeq = model.getCurrentItem();
		
		final View layoutPrev = findViewById(R.id.layout_prev);
		final View layoutNext = findViewById(R.id.layout_next);
		final View layoutNow = findViewById(R.id.layout_now);
		
	
		final TpdlActivity curItem = mTripDetailList.get(itemSeq);
		final TextView dispCurName = (TextView) layoutNow.findViewById(R.id.text_current_name);
		if(curItem instanceof ResortActivity){
			Log.v(UI_MODE_SERVICE, curItem.getName());
			//dispCurName.sett
			dispCurName.setText(curItem.getName());
		}
		else if (curItem instanceof TransitActivity){
			dispCurName.setText(curItem.getName());
		}
		
		
		if(itemSeq > 0) {
			final TpdlActivity item = mTripDetailList.get(itemSeq - 1);
			final TextView dispName = (TextView) layoutPrev.findViewById(R.id.text_trip);
			final TextView dispDetail = (TextView) layoutPrev.findViewById(R.id.text_detail);
			if(item instanceof ResortActivity){
				dispName.setText(item.getName());
				dispDetail.setText(item.getAddress());
			}
			else if (item instanceof TransitActivity){
				dispName.setText(item.getName());
				dispDetail.setText(item.getAddress());
			}
		}
		
		if(itemSeq < mTripDetailList.size() - 1) {
			final TpdlActivity item = mTripDetailList.get(itemSeq + 1);
			final TextView dispName = (TextView) layoutNext.findViewById(R.id.text_trip);
			final TextView dispDetail = (TextView) layoutNext.findViewById(R.id.text_detail);
			if(item instanceof ResortActivity){
				dispName.setText(item.getName());
				dispDetail.setText(item.getAddress());	
			}
			else if (item instanceof TransitActivity){
				dispName.setText(item.getName());
				dispDetail.setText(item.getAddress());
			}
		}	
	}
	
	private class RunReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			final String result = intent.getStringExtra(Constants.EXTRA_RESULT);
			setup(result);
		}
		
	}
}
